<?php

namespace App\Jobs;

use App\Enums\OrderEnum;
use App\Http\Logic\OrderLogic;
use App\Logic\BaoJianMachineLogic;
use App\Models\SwtOrder;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CancelOrder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $order_id;

    public $order_type;

    //处理次数
    public $tries = 5;

    public $timeout = 300;

    /**
     * CancelOrder constructor.
     * @param $id
     */
    public function __construct($id, $minutes, $order_type)
    {
        $this -> order_id = $id;

        $this -> order_type = $order_type;

        $this -> delay( now() -> addMinutes( $minutes ) );

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            $order_info = SwtOrder::with('machine') -> firstWhere('id', $this->order_id);

            //根据传递时的订单状态做出超时处理
            switch ($this -> order_type) {
                case OrderEnum::ORDER_CREATED :
                    if($order_info -> state == OrderEnum::ORDER_CREATED) {
                        echo "订单号： ".$this->order_id." 未授权取消";
                        OrderLogic::cancelOrder($order_info -> order_sn, $order_info -> door_num, $order_info -> platform);
                    }
                    break;
                case OrderEnum::ORDER_DOING :
                    if($order_info -> state == OrderEnum::ORDER_DOING){
                        $warning_desc = "超时未收到回调";
                        if($order_info -> video_status == 1 && $order_info -> machine -> source_type == 2) {
                            //如果是因为视频没回调的订单
                            $res = BaoJianMachineLogic::finishRecognitionOrder($order_info -> order_sn);
                            if($res) {
                                echo "订单二次提交完结成功";
                            } else {
                                $warning_desc = BaoJianMachineLogic::getErrorMessage();
                                echo "订单号： ".$this->order_id." ".$warning_desc;
                            }
                        } else {
                            echo "订单号： ".$this->order_id." 购买超时取消";
                            OrderLogic::cancelOrder($order_info -> order_sn, $order_info -> door_num, $order_info -> platform);
                            OrderLogic::setWarningOrder($order_info -> order_sn, $order_info -> door_num,$warning_desc);
                        }
                    }
                   break;
                case OrderEnum::ORDER_REVOKED :
                    echo "订单号： ".$this->order_id." 已自动取消";
                break;
                case OrderEnum::ORDER_DONE :
                    echo "订单号： ".$this->order_id." 已完成，无法取消";
                break;
                default :
                    echo "订单号： ".$this->order_id." 未执行任何操作";
                break;
            }
        } catch (\Exception $e) {
            $this->fail($e -> getMessage());
        }
    }
}
